package com.fedming.bottomnavigationdemo.Util;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class CheckSettingUtils {
    /*
    检查是否手动配置了设置文件，什么情况下才算配置了设置文件呢？
  1.data目录下有setting
  2.uuid不是空的

     */
    public static boolean isInitSetting(Context context){

        for (String file: context.fileList()){ //遍历文件名

            if(file.equals("setting")){
                JSONObject settingJson = JSONObject.parseObject(load(context,"setting"));
                if (settingJson.getString("uuid").equals("")){

                    return false;
                }
                else{
                    return true;
                }

            }
        }
        //表明data目录下不存在名为setting的文件
        return false;
    }
    public static String load(Context context,String file_name) {  //加载内部存储中的指定文件
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            in = context.openFileInput(file_name);
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }
}
